package com.orangelife.mobile.verify.biz;

import android.os.Handler;
import android.os.Message;
import android.widget.EditText;
import android.widget.TextView;

import com.common.sp.UserSp;
import com.curry.android.http.OrangeErrorHandler;
import com.curry.android.util.CountDownHandler;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.login.biz.LoginBiz;

/**
 * <p>
 * Created by 吴格非 on 2017-1-6.
 * <p>
 *
 * @author 吴格非
 * @since v1.0.0
 */

public class VerifyCodeBiz {

    /** 默认发送短信 */
    private boolean isSendSms = true;
    private CountDownHandler countDownHandler;
    private boolean isCountDown;
    private OrangeLifeBaseActivity context;
    private TextView tvGetCode;
    private TextView tvVoiceCode;
    private EditText etVerifyCode;

    public VerifyCodeBiz(OrangeLifeBaseActivity context, TextView tvGetCode, TextView tvVoiceCode, EditText etVerifyCode) {
        this.context = context;
        this.tvGetCode = tvGetCode;
        this.tvVoiceCode = tvVoiceCode;
        this.etVerifyCode = etVerifyCode;
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case SmsHttpBiz.WHAT_SMS:
                    handleResponseCode(msg, context.getResources().getString(R.string.send_sms_fail), context.getResources().getString(
                            R.string.send_sms_success));
                    break;
                case SmsHttpBiz.WHAT_VOICE_CODE:
                    handleResponseCode(msg, context.getResources().getString(R.string.send_voice_fail), context.getResources().getString(
                            R.string.send_voice_success));
                    break;
                default:
                    break;
            }
        }

    };

    private void handleResponseCode(Message msg, String okMsg, String failMsg) {
        //改为通知Activity去显示
        context.closeDialog();
        boolean isOk2 = OrangeErrorHandler.getInstance().isSuccess(msg.obj, okMsg, failMsg);
        if (!isOk2) {
            stopTimer();
        }
    }

    public void onClickGetCode(boolean isSendSms) {
        String phone = UserSp.getRegPhone();
        disableCode();
        if (LoginBiz.validatePhoneNum(phone, context)) {
            context.showDialog();
            if (isSendSms) {
                this.isSendSms = true;
                requestSmsCode(phone);
            } else {
                this.isSendSms = false;
                requestVoiceCode(phone);
            }
            startTimer("60");
        } else {
            enableCode();
        }
    }

    /** 请求短信验证码 */
    private void requestSmsCode(String phone) {
        SmsHttpBiz.getSmsCode(phone, mHandler, SmsHttpBiz.WHAT_SMS);
    }

    /** 请求语音验证码 */
    private void requestVoiceCode(String phone) {
        SmsHttpBiz.getVoiceCode(phone, mHandler);
    }

    private void disableCode() {
        etVerifyCode.setText("");
        tvGetCode.setEnabled(false);
        tvVoiceCode.setEnabled(false);
    }

    private void enableCode() {
        tvGetCode.setEnabled(true);
        tvVoiceCode.setEnabled(true);
        tvVoiceCode.setText(context.getResources().getString(
                R.string.voice_code));
        tvGetCode.setText(context.getResources().getString(R.string.send_again));
    }

    //初始化
    private void startTimer(String second) {
        if (countDownHandler == null) {
            countDownHandler = new CountDownHandler();
            countDownHandler.isOnlySecond(true);
            countDownHandler.isUseCustomCountDownInfo(true);
            countDownHandler.setOnCountDownTimerFinishedListener(new CountDownHandler.OnCountDownTimerFinishedListener() {
                @Override
                public void onFinish() {
                    enableCode();
                }

                @Override
                public void onCountDown(String currentSecond) {
                    String str = context.getResources().getString(R.string.count_down_info, currentSecond);
                    if (isSendSms) {
                        tvGetCode.setText(str);
                        etVerifyCode.setHint("请输入短信验证码");
                    } else {
                        tvVoiceCode.setText(str);
                        etVerifyCode.setHint("请输入语音验证码");
                    }
                }
            });
        }
        countDownHandler.startTimer(second);
    }

    private void stopTimer() {
        if (countDownHandler != null && isCountDown) {
            countDownHandler.stopTimer();
            isCountDown = false;
        }
    }
}
